package com.jeesite.modules.exam.quota.service;

import java.util.List;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.backlog.service.DcSupBacklogService;
import com.jeesite.modules.exam.notice.entity.DcSupExamNotice;
import com.jeesite.modules.exam.notice.service.DcSupExamNoticeService;
import com.jeesite.modules.exam.quota.entity.DcSupExamQuotaInfo;
import com.jeesite.modules.exam.quota.entity.DcSupExamSumQuotaInfo;
import com.jeesite.modules.msg.entity.content.PcMsgContent;
import com.jeesite.modules.msg.utils.MsgPushUtils;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.yzw.entity.MsgDetail;
import com.jeesite.modules.yzw.service.LehandYzwPushTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.exam.quota.entity.DcSupExamQuota;
import com.jeesite.modules.exam.quota.dao.DcSupExamQuotaDao;
import com.jeesite.modules.file.utils.FileUploadUtils;

import javax.validation.constraints.Size;

/**
 * 考核任务指标表Service
 * @author xhc
 * @version 2022-05-28
 */
@Service
@Transactional(readOnly=false)
public class DcSupExamQuotaService extends CrudService<DcSupExamQuotaDao, DcSupExamQuota> {
	@Autowired
	private UserService userService;
	@Autowired
	private LehandYzwPushTask lehandYzwPushTask;
	@Autowired
	private DcSupExamNoticeService dcSupExamNoticeService;
	@Autowired
	private DcSupBacklogService dcSupBacklogService;
	/**
	 * 获取单条数据
	 * @param dcSupExamQuota
	 * @return
	 */
	@Override
	public DcSupExamQuota get(DcSupExamQuota dcSupExamQuota) {
		return super.get(dcSupExamQuota);
	}
	
	/**
	 * 查询分页数据
	 * @param dcSupExamQuota 查询条件
	 * @param dcSupExamQuota.page 分页对象
	 * @return
	 */
	@Override
	public Page<DcSupExamQuota> findPage(DcSupExamQuota dcSupExamQuota) {
		return super.findPage(dcSupExamQuota);
	}
	
	/**
	 * 查询列表数据
	 * @param dcSupExamQuota
	 * @return
	 */
	@Override
	public List<DcSupExamQuota> findList(DcSupExamQuota dcSupExamQuota) {
		return super.findList(dcSupExamQuota);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param dcSupExamQuota
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(DcSupExamQuota dcSupExamQuota) {
		super.save(dcSupExamQuota);
		// 保存上传图片
		FileUploadUtils.saveFileUpload(dcSupExamQuota, dcSupExamQuota.getId(), "dcSupExamQuota_image");
		// 保存上传附件
		FileUploadUtils.saveFileUpload(dcSupExamQuota, dcSupExamQuota.getId(), "dcSupExamQuota_file");
		String noticeId = dcSupExamQuota.getNoticeId();
		DcSupExamNotice dcSupExamNotice = dcSupExamNoticeService.get(noticeId);
		//报送发通知
		if(StringUtils.equals("2",dcSupExamQuota.getSubmitStatus())) {
			new Thread(() -> {
				User user = new User();
				user.setRoleCode("OTS_ORG");
				user.setUserType("employee");
				user.setUserCode(dcSupExamNotice.getCreateBy());
				List<User> list = userService.findList(user);
				String msg = "";
				String org = dcSupExamQuota.getCreateOrgName();
				String bizType = "";
				if ("1".equals(dcSupExamQuota.getQuotaGroup())) {
					msg = org + "单位的关键指标已提交，待审核，请及时登录系统查看";
					bizType = "exam_audit_key";
					//处理了发通知和驳回通知
					dcSupBacklogService.doBacklog(dcSupExamQuota.getExamId(),"exam_submit_key",dcSupExamQuota.getCreateOrg());
				} else {
					msg =  org + "单位的单项指标已提交，待审核，请及时登录系统查看";
					bizType = "exam_audit_dan";
					//处理了发通知和驳回通知
					dcSupBacklogService.doBacklog(dcSupExamQuota.getExamId(),"exam_submit_dan",dcSupExamQuota.getCreateOrg());
				}
				for (User user_ : list) {
					//处理发信息逻辑
//					PcMsgContent pcMsgContent = new PcMsgContent();
//					pcMsgContent.setTitle("提示信息");
//					pcMsgContent.setContent(msg);
//					MsgPushUtils.push(pcMsgContent, dcSupExamQuota.getId(), bizType, user_.getUserCode());
					//发送易政网消息
					MsgDetail msgDetail = new MsgDetail();
					msgDetail.setSendid(UserUtils.getUser().getUserCode());
					msgDetail.setSubjectid(user_.getUserCode());
					msgDetail.setMesg(msg);
					lehandYzwPushTask.pushScoreShortMsg(msgDetail);

					if ("1".equals(dcSupExamQuota.getQuotaGroup())) {
						dcSupBacklogService.add("目标考核","关键指标审核","关键指标报送审核","提示信息"
								,msg.replace("登录系统","")
								,"/a/targetAudit/targetSendAuditPage?quotaGroup=1&examId="+dcSupExamQuota.getExamId(),dcSupExamQuota.getCreateOrg()
								,dcSupExamQuota.getExamId(),bizType,user.getUserCode()
								,"","");
					} else {
						dcSupBacklogService.add("目标考核","单项指标审核","单项指标报送审核","提示信息"
								,msg.replace("登录系统","")
								,"/a/targetAudit/targetSendDXAuditPage?quotaGroup=2&examId="+dcSupExamQuota.getExamId(),dcSupExamQuota.getCreateOrg()
								,dcSupExamQuota.getExamId(),bizType,user.getUserCode()
								,"","");
					}

				}
			}).start();
		}

	}
	@Transactional(readOnly=false)
	public void save2(DcSupExamQuota dcSupExamQuota) {
		super.save(dcSupExamQuota);
		// 保存上传图片
		FileUploadUtils.saveFileUpload(dcSupExamQuota, dcSupExamQuota.getId(), "dcSupExamQuota_image");
		// 保存上传附件
		FileUploadUtils.saveFileUpload(dcSupExamQuota, dcSupExamQuota.getId(), "dcSupExamQuota_file");
		String noticeId = dcSupExamQuota.getNoticeId();
		DcSupExamNotice dcSupExamNotice = dcSupExamNoticeService.get(noticeId);
		//报送发通知
		if(StringUtils.equals("2",dcSupExamQuota.getSubmitStatus())) {
			new Thread(() -> {
				User user = new User();
				user.setRoleCode("OTS_ORG");
				user.setUserType("employee");
				user.setUserCode(dcSupExamNotice.getCreateBy());
				List<User> list = userService.findList(user);
				String msg = "";
				String org = dcSupExamQuota.getCreateOrgName();
				String bizType = "";
				if ("1".equals(dcSupExamQuota.getQuotaGroup())) {
					msg = org + "单位的关键指标已提交，待审核，请及时登录系统查看";
					bizType = "exam_audit_key";
					//处理了发通知和驳回通知
					dcSupBacklogService.doBacklog(dcSupExamQuota.getExamId(),"exam_submit_key",dcSupExamQuota.getCreateOrg());
				} else {
					msg = org + "单位的单项指标已提交，待审核，请及时登录系统查看";
					bizType = "exam_audit_dan";
					//处理了发通知和驳回通知
					dcSupBacklogService.doBacklog(dcSupExamQuota.getExamId(),"exam_submit_dan",dcSupExamQuota.getCreateOrg());
				}
				for (User user_ : list) {
					//处理发信息逻辑
//					PcMsgContent pcMsgContent = new PcMsgContent();
//					pcMsgContent.setTitle("提示信息");
//					pcMsgContent.setContent(msg);
//					MsgPushUtils.push(pcMsgContent, dcSupExamQuota.getId(), bizType, user_.getUserCode());
					//发送易政网消息
					MsgDetail msgDetail = new MsgDetail();
					msgDetail.setSendid(UserUtils.getUser().getUserCode());
					msgDetail.setSubjectid(user_.getUserCode());
					msgDetail.setMesg(msg);
					lehandYzwPushTask.pushScoreShortMsg(msgDetail);

					if ("1".equals(dcSupExamQuota.getQuotaGroup())) {
						dcSupBacklogService.add("目标考核","关键指标审核","关键指标报送审核","提示信息"
								,msg.replace("登录系统","")
								,"/a/targetAudit/targetSendAuditPage?quotaGroup=1&examId="+dcSupExamQuota.getExamId(),dcSupExamQuota.getCreateOrg()
								,dcSupExamQuota.getExamId(),bizType,user.getUserCode()
								,"","");
					} else {
//						dcSupBacklogService.add("目标考核","单项指标审核","单项指标报送审核","提示信息"
//								,msg
//								,"/a/targetAudit/targetSendDXAuditPage?quotaGroup=2&examId="+dcSupExamQuota.getExamId(),dcSupExamQuota.getCreateOrg()
//								,dcSupExamQuota.getExamId(),bizType,user.getUserCode()
//								,"","");
					}

				}
			}).start();
		}

	}
	
	/**
	 * 更新状态
	 * @param dcSupExamQuota
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(DcSupExamQuota dcSupExamQuota) {
		super.updateStatus(dcSupExamQuota);
	}
	
	/**
	 * 删除数据
	 * @param dcSupExamQuota
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(DcSupExamQuota dcSupExamQuota) {
		super.delete(dcSupExamQuota);
	}

    public Integer findSumQuotaIsDanExist(String examId) {
		return dao.findSumQuotaIsDanExist(examId);
    }

    public void updateFileStatusById(String quotaId,String status) {
		dao.updateFileStatusById(quotaId,status);
    }

	public List<DcSupExamQuota> getExamQuota(DcSupExamQuota dcSupExamQuota) {
		return dao.getExamQuota(dcSupExamQuota);
	}

    public DcSupExamQuota getDcSupExamQuota(DcSupExamQuota dcSupExamQuota) {
		return dao.getDcSupExamQuota(dcSupExamQuota);
    }
	@Transactional(readOnly=false)
	public void deleteDcSupExamQuotaId(String dcSupExamQuotaId) {
		 dao.deleteDcSupExamQuotaInfo(dcSupExamQuotaId);
		 dao.deleteDcSupExamQuotaOrg(dcSupExamQuotaId);
		 dao.deleteDcSupExamQuotaId(dcSupExamQuotaId);
	}

    public DcSupExamSumQuotaInfo getByExamIdAndCreateorg(DcSupExamQuota dcSupExamQuota) {
		return dao.getByExamIdAndCreateorg(dcSupExamQuota);
    }
	@Transactional(readOnly=false)
	public void deleteDcSupExamQuotaInfoAndOrg(String dcSupExamQuotaId) {
		dao.deleteDcSupExamQuotaInfo(dcSupExamQuotaId);
		dao.deleteDcSupExamQuotaOrg(dcSupExamQuotaId);
	}
}